package com.shop.admin.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.TakeoutGoodsDao;
import com.shop.admin.entity.*;
import com.shop.admin.service.TakeoutGoodsAttributeService;
import com.shop.admin.service.TakeoutGoodsService;
import com.shop.admin.service.TakeoutGoodsSpecificationService;
import com.shop.admin.service.TakeoutGoodsSpecificationValueService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.vo.TakeoutGoodsSpecificationVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TakeoutGoodsServiceImpl extends ServiceImpl<TakeoutGoodsDao, TakeoutGoods> implements TakeoutGoodsService {

    @Resource
    private TakeoutGoodsDao takeoutGoodsDao;

    @Resource
    private TakeoutGoodsAttributeService takeoutGoodsAttributeService;

    @Resource
    private TakeoutGoodsSpecificationService takeoutGoodsSpecificationService;

    @Resource
    private TakeoutGoodsSpecificationValueService takeoutGoodsSpecificationValueService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        System.out.println(params.get("merchantId"));
        Page<TakeoutGoods> page = new Page<>(Long.parseLong(params.get("page").toString()),Long.valueOf(params.get("limit").toString()) );
        Page<TakeoutGoods> resPage = takeoutGoodsDao.pageTakeoutGoodsDesc(page,params);
        PageUtils pageUtils = new PageUtils(resPage);
        return pageUtils;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        for (Long aLong : asList) {
            this.baseMapper.delete(new QueryWrapper<TakeoutGoods>().lambda().eq(TakeoutGoods::getId,aLong));
        }
    }

    @Override
    public TakeoutGoodsSpecificationVo takeoutGoodsById(Integer takeoutGoodsId) {
        List<TakeoutGoodsAttribute> takeoutGoodsAttributes = takeoutGoodsAttributeService.list(new QueryWrapper<TakeoutGoodsAttribute>().lambda().eq(TakeoutGoodsAttribute::getGoodsId, takeoutGoodsId));
        List<TakeoutGoodsSpecification> takeoutGoodsSpecifications = takeoutGoodsSpecificationService.list(new QueryWrapper<TakeoutGoodsSpecification>().lambda().eq(TakeoutGoodsSpecification::getGoodsId, takeoutGoodsId));
        //List<String> specifications = takeoutGoodsSpecifications.stream().map(i -> i.getSpecification()).collect(Collectors.toList());
        List<Integer> integers = takeoutGoodsSpecifications.stream().map(i -> i.getId()).collect(Collectors.toList());
        List<TakeoutGoodsSpecificationValue> takeoutGoodsSpecificationValues = takeoutGoodsSpecificationValueService.list(new QueryWrapper<TakeoutGoodsSpecificationValue>().lambda().in(TakeoutGoodsSpecificationValue::getSpecificationId, integers));
        Map<Integer, List<TakeoutGoodsSpecificationValue>> TakeoutGoodsSpecificationValueMap = takeoutGoodsSpecificationValues.stream().collect(Collectors.groupingBy(TakeoutGoodsSpecificationValue::getSpecificationId));
        List<Map<String, List<TakeoutGoodsSpecificationValue>>> collect = takeoutGoodsSpecifications.stream().map(item -> {
            Map<String, List<TakeoutGoodsSpecificationValue>> map = new HashMap<>();
            TakeoutGoodsSpecificationValueMap.forEach((k, v) -> {
                if (k.equals(item.getId())) {
                    map.put(item.getSpecification(), v);
                }
            });
            return map;
        }).collect(Collectors.toList());
        Map<String, List<TakeoutGoodsSpecificationValue>> stringListMap = collect.get(0);
        TakeoutGoodsSpecificationVo TakeoutGoodsSpecificationVo = new TakeoutGoodsSpecificationVo();
        TakeoutGoodsSpecificationVo.setTakeoutGoodsAttributes(takeoutGoodsAttributes);
        TakeoutGoodsSpecificationVo.setTakeoutGoodsSpecificationValueMap(stringListMap);
        return TakeoutGoodsSpecificationVo;
    }
}
